//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.Text;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.ComponentModel;
using Tathata.Common;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

namespace Tathata.Web.Modules.Products
{
    public partial class ProductEmailAFriend : BaseNopUserControl
    {
        private void ApplyLocalization()
        {
            btnEmail.Text = GetLocaleResourceString("ProductEmailAFriendControl.EmailButton");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ApplyLocalization();

            if (!SettingManager.GetSettingValueBoolean("Common.EnableEmailAFirend"))
                Response.Redirect("~/Default.aspx");

            if (!Page.IsPostBack)
                BindData();
        }

        protected void BindData()
        {
            Product product = ProductManager.GetByProductID(ProductID);
            if (product != null)
            {
                hlProduct.NavigateUrl = SEOHelper.GetProductURL(product.ProductID);
                hlProduct.Text = product.Name;
                lShortDescription.Text = product.ShortDescription;

                if (NopContext.Current.User == null)
                {
                    lblEmailAFriend.Text = GetLocaleResourceString("ProductEmailAFriendControl.OnlyRegisteredUsersCanEmailAFriend");
                    txtFriendsEmail.Enabled = false;
                    txtPersonalMessage.Enabled = false;
                    btnEmail.Enabled = false;
                }
                else
                {
                    lblEmailAFriend.Text = string.Empty;
                    txtFriendsEmail.Enabled = true;
                    lblFrom.Text = NopContext.Current.User.Email;
                    txtPersonalMessage.Enabled = true;
                    btnEmail.Enabled = true;
                }
            }
            else
                this.Visible = false;
        }

        protected void btnEmail_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Product product = ProductManager.GetByProductID(this.ProductID);
                    if (product != null)
                    {
                        if (NopContext.Current.User == null)
                        {
                            lblEmailAFriend.Text = GetLocaleResourceString("ProductEmailAFriendControl.OnlyRegisteredUsersCanEmailAFriend");
                            return;
                        }

                        MessageManager.SendEmailAFriendMessage(NopContext.Current.User, product, txtFriendsEmail.Text, txtPersonalMessage.Text);

                        txtFriendsEmail.Text = string.Empty;
                        txtPersonalMessage.Text = string.Empty;
                        lblEmailAFriend.Text = GetLocaleResourceString("ProductEmailAFriendControl.YourMessageHasBeenSent");
                    }
                    else
                        Response.Redirect("~/Default.aspx");
                }
                catch (Exception exc)
                {
                    lblEmailAFriend.Text = exc.Message;
                }
            }
        }

        public int ProductID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductID");
            }
        }
    }
}